package com.tianjipms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianjipms.entity.Building;
import com.tianjipms.entity.House;
import com.tianjipms.entity.User;
import com.tianjipms.service.BuildingService;
import com.tianjipms.service.HouseService;
import com.tianjipms.service.UserService;
import com.tianjipms.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/house")
public class HouseController {
    @Resource
    private HouseService houseService;
    @Resource
    private BuildingService buildingService;
    @Resource
    private UserService userService;
    //添加房屋
    @PostMapping("/add")
    public Result addHouse(@RequestBody House house){
        Building building = buildingService.getOne(new QueryWrapper<Building>().eq("id",house.getBuildingId()));
        if (building.getResidentNum() == building.getHouseNum()){
            return Result.error().message("住户已满");
        }
        if(houseService.existsByHouseNumber(house.getHouseNumber())){
            return Result.error().message("房屋已存在");
        }
        if (!userService.existsByuserPhone(house.getUserPhone())){
            return Result.error().message("用户不存在");
        }
        house.setUserId(userService.findUserByPhone(house.getUserPhone()).getId());
        houseService.save(house);
        buildingService.updateResidentNumById(building);
        return Result.ok();
    }
    //分页查询所有房屋信息
    @GetMapping("/listAll")
    public Result listAll(@RequestParam Long buildingId,
                          @RequestParam(defaultValue = "1") Long pageNum,
                          @RequestParam(defaultValue = "5") Long pageSize){
        IPage<House> page = new Page<>(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("building_id",buildingId);
        houseService.page(page, queryWrapper);
        return Result.ok(page);
    }
    //分页查询用户房屋信息
    @GetMapping("/listAllByUserId")
    public Result listAllByUserId(@RequestParam Long userId,
                          @RequestParam(defaultValue = "1") Long pageNum,
                          @RequestParam(defaultValue = "5") Long pageSize){
        IPage<House> page = new Page<>(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        houseService.page(page, queryWrapper);
        return Result.ok(page);
    }

    //更换业主
    @PostMapping("/updateByUserId")
    public Result updateByuserId(@RequestBody House house){
        if (!userService.existsByuserPhone(house.getUserPhone())){
            return Result.error().message("用户不存在");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        User user = userService.getOne(queryWrapper.eq("phone", house.getUserPhone()));
        House newHouse = houseService.getOne(new QueryWrapper<House>().eq("id", house.getId()));
        newHouse.setUserId(user.getId());
        houseService.updateById(newHouse);
        return Result.ok();
    }

    //删除房屋
    @DeleteMapping("/delete/{id}")
    public Result deleteHouse(@PathVariable Long id){
        return Result.ok(houseService.removeById(id));
    }
}
